package GUILayer;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import CtrlLayer.PersonCtrl;

@SuppressWarnings("serial")
public class PopupOpretLeverandr extends PopupWindow {
	private PersonCtrl PC;
	private JTextField textFirmanavn;
	private JTextField textKontaktperson;
	private JTextField textAddress;
	private JTextField textpostal;
	private JTextField textCity;
	private JTextField textPhone;
	private JTextField textEmail;
	private JTextField textCreditornr;

	public PopupOpretLeverandr(Updateable mother) //throws HeadlessException - why?
	{
		super(mother);
		PC = new PersonCtrl();
		getContentPane().setPreferredSize(new Dimension(440,350));
		setTitle("Opret Leverandoer");

		JLabel lblFirmaNavn = new JLabel("Firmanavn");
		JLabel lblKontaktperson = new JLabel("Kontaktperson");
		JLabel lblAddress = new JLabel("Adresse");
		JLabel lblpostal = new JLabel("Postnr");
		JLabel lblCity = new JLabel("By");
		JLabel lblPhone = new JLabel("Telefon");
		JLabel lblEmail = new JLabel("Email");
		JLabel lblCreditorNr = new JLabel("Creditor nr");

		JButton btnAnnuller = new JButton("Annuller");
		btnAnnuller.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				quit();
			}
		});
		JButton btnOpret = new JButton("Opret");
		btnOpret.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				opretLeverandr();
				getMother().update();
				quit();
			}
		});
		
		JButton btnRydFelter = new JButton("Ryd Felter");
		btnRydFelter.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				clearFields();
			}
		});

		textFirmanavn = new JTextField();
		textFirmanavn.setColumns(10);

		textKontaktperson = new JTextField();
		textKontaktperson.setColumns(10);

		textAddress = new JTextField();
		textAddress.setColumns(10);

		textpostal = new JTextField();
		textpostal.setColumns(10);

		textCity = new JTextField();
		textCity.setColumns(10);

		textPhone = new JTextField();
		textPhone.setColumns(10);

		textEmail = new JTextField();
		textEmail.setColumns(10);

		textCreditornr = new JTextField();
		textCreditornr.setColumns(10);

		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
							.addGap(23)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblEmail)
								.addComponent(lblFirmaNavn)
								.addComponent(lblKontaktperson)
								.addComponent(lblAddress)
								.addComponent(lblpostal)
								.addComponent(lblCity, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblPhone)
								.addComponent(lblCreditorNr))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(textCreditornr, GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)
								.addComponent(textPhone, 264, 264, 264)
								.addComponent(textCity, 264, 264, 264)
								.addComponent(textpostal, 264, 264, 264)
								.addComponent(textAddress, 264, 264, 264)
								.addComponent(textKontaktperson, 264, 264, 264)
								.addComponent(textFirmanavn, 264, 264, 264)
								.addComponent(textEmail, GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE)))
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addComponent(btnRydFelter)
							.addGap(18)
							.addComponent(btnAnnuller)
							.addGap(18)
							.addComponent(btnOpret)))
					.addGap(30))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblFirmaNavn)
						.addComponent(textFirmanavn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblKontaktperson)
						.addComponent(textKontaktperson, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblAddress)
						.addComponent(textAddress, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblpostal, GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE)
						.addComponent(textpostal, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblCity)
						.addComponent(textCity, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblPhone)
						.addComponent(textPhone, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblEmail)
						.addComponent(textEmail, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblCreditorNr)
						.addComponent(textCreditornr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(23)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addComponent(btnOpret)
						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
							.addComponent(btnAnnuller)
							.addComponent(btnRydFelter)))
					.addContainerGap())
		);

		getContentPane().setLayout(groupLayout);
		pack();
	}

	private void opretLeverandr() // indlaeser text fra skaermen
	{
		String companyName = textFirmanavn.getText();
		String name = textKontaktperson.getText();
		String address = textAddress.getText();
		String postal = textpostal.getText();
		String city = textCity.getText();
		String phone = textPhone.getText();
		String email = textEmail.getText();
		int creditornr = Integer.parseInt(textCreditornr.getText());
		PC.createSupplier(name,address,postal,city,phone,email,companyName,creditornr);
	}

	public void clearFields()
	{
		textFirmanavn.setText(null);
		textKontaktperson.setText(null);
		textAddress.setText(null);
		textpostal.setText(null);
		textCity.setText(null);
		textPhone.setText(null);
		textEmail.setText(null);
		textCreditornr.setText(null);
	}

}
